package com.test.burnincash.aaahpoppomelo.bean;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.test.burnincash.util_burnin.StringUtil_burnin;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Comparator;

public class Order_hpoppomelo implements Serializable {

    private int orderNumber;
    private String date;
    private String customer;
    private ArrayList<OrderItem_hpoppomelo> items;

    public static Order_hpoppomelo optionOrder = new Order_hpoppomelo(0,"","");

    private Order_hpoppomelo() {
        this.orderNumber = 0;
        this.date = "";
        this.customer = "";
        this.items = new ArrayList<>();
    }

    public Order_hpoppomelo(int orderNumber, String date, String customer) {
        this.orderNumber = orderNumber;
        this.date = date;
        this.customer = customer;
        this.items = new ArrayList<>();
    }

    public static Order_hpoppomelo parse(String jsonStr){

        Order_hpoppomelo instance = new Order_hpoppomelo();
        try {
            JSONObject jsonObject = new JSONObject(jsonStr);
            instance.orderNumber = jsonObject.optInt("orderNumber");
            instance.date = jsonObject.optString("date");
            instance.customer = jsonObject.optString("customer");
            JSONArray jsonArray = jsonObject.optJSONArray("items");
            for (int i=0;i<jsonArray.length();i++){
                instance.addItem(new Gson().fromJson(jsonArray.optJSONObject(i).toString(),OrderItem_hpoppomelo.class));
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
        return instance;
    }

    public int getOrderNumber() {
        return orderNumber;
    }

    public String getDate() {
        return StringUtil_burnin.getSafeString(date);
    }

    public String getCustomer() {
        return StringUtil_burnin.getSafeString(customer);
    }

    public ArrayList<OrderItem_hpoppomelo> getItems() {
        if (items==null) return new ArrayList<>();
        items.sort(Comparator.comparingInt(OrderItem_hpoppomelo::getNo));
        return items;
    }

    public void setOrderNumber(int orderNumber) {
        this.orderNumber = orderNumber;
    }

    public void setDate(String date) {
        this.date = StringUtil_burnin.getSafeString(date);
    }

    public void setCustomer(String customer) {
        this.customer = StringUtil_burnin.getSafeString(customer);
    }

    public void addItem(OrderItem_hpoppomelo item){
        items.add(item);
    }

    public boolean containsItemName(String itemName){
        for (OrderItem_hpoppomelo item:items){
            if (item.getItemName().equalsIgnoreCase(itemName)) return true;
        }
        return false;
    }

    public boolean isDownloaded(){
        return false;
    }

    public JsonElement toJson(){

        Gson gson = new Gson();
        JsonObject jsonObject = new JsonObject();

        jsonObject.add("orderNumber",gson.toJsonTree(orderNumber));
        jsonObject.add("date",gson.toJsonTree(date));
        jsonObject.add("customer",gson.toJsonTree(customer));

        JsonArray jsonArray = new JsonArray();
        for (OrderItem_hpoppomelo item:items){
            jsonArray.add(item.toJson());
        }
        jsonObject.add("items",jsonArray);

        return jsonObject;

    }

}
